package com.atguigu.chap5;


import java.util.concurrent.*;

public class MyThreadPool {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        // 创建线程池
        ExecutorService executorService = new ThreadPoolExecutor(
                2, 5, 2,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(10),
                Executors.defaultThreadFactory(),
                //new ThreadPoolExecutor.DiscardOldestPolicy() //抛弃队列中最老的任务
                //new ThreadPoolExecutor.CallerRunsPolicy() //调用者线程执行
                //new ThreadPoolExecutor.AbortPolicy()//默认策略
                //new ThreadPoolExecutor.DiscardPolicy()//抛弃当前任务
                (r, executor) -> {
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    executor.submit(r);
                }
        );
        for (int i = 1; i <= 16; i++) {
            String number = String.valueOf(i);
            executorService.submit(() -> {
                System.out.println(Thread.currentThread().getName() + "  ===========>   方法被执行   number=" + number);
                return 100;
            });

        }
    }
}
